** This do-file extracts parliamentary election results from ParlGov

import excel using "$project_path/data/1_input/elections/PARLGOV/PARLGOV_Main.xlsx", sheet("election") firstrow clear

// Excluding European Parliament elections
keep if election_type=="parliament"

// Keeping only elections between 1946 and 2018
gen Year=substr(election_date,1,strpos(election_date,"-")-1)
destring Year, replace
keep if Year>1945 & Year<=2018

// Sort by number of seats won 
gsort country_name election_date -seats

// Reshape to keep one election per observation
drop previous_parliament_election_id previous_cabinet_id left_right party_id
order election_id country_name election_date
by country_name election_date : gen id = _n
reshape wide vote_share seats party_name_short-party_name_english, i(country_name election_id) j(id)

// Month variable
gen Month = substr(election_date,6,2)
destring Month, replace
bys country_name Year: gen temp = _N
replace Month = . if temp == 1
drop temp

// Adjusting party names
forvalues i=1/22 {
	replace party_name_short`i' = "("+party_name_short`i'+")"
	replace party_name_short`i' = "" if party_name_short`i'=="()"
	gen same_local_english_`i'=1 if party_name`i'==party_name_english`i'
	gen Party_`i' = party_name_english`i'
	// Adding local name if necessary 
	replace Party_`i' = Party_`i' + " [" + party_name`i' +"]" if same_local_english_`i'!=1
	// Adding abbreviation
	replace Party_`i' = Party_`i' + " " + party_name_short`i' if party_name_short`i'!="" 
}
drop same_local_english* party_name_short* party_name_english* party_name*

drop country_id country_name_short election_type election_id election_date
ren country_name Country
gen Type_Election = "Parliamentary"
ren seats_total Total_Seats
forvalues i=1/22{
	ren seats`i' Seats_`i'
}

// Reordering parties
sort Country Year Month
reshape long Party_ Seats_ vote_share, i(Country Year Month) j(oldid)
gen temp = 1 if Party_==""
replace temp = 0 if temp==.
gsort Country Year Month -Seats_ -vote_share temp Party_
drop temp
by Country Year Month : gen id=_n
drop oldid
reshape wide Party_ Seats_ vote_share, i(Country Year Month) j(id)
drop vote_share*

// Creating missing variables to facilitate merge downstream
forvalues k = 1/22 {
	gen Seat_Share_`k' = .
}
forvalues k = 23/75 {
	gen Party_`k' = ""
	foreach v in Seats_ Seat_Share_ {
		gen `v'`k' = .
	}
}
recast double Seat_Share_*
foreach v in flag_wrong_sum_seats flag_inconsequential flag_vacant_seats flag_vacant_seats_nb flag_appointed flag_appointed_nb flag_non_partisan flag_coup {
	gen `v' = .
}
gen flag_inconsequential_note = ""
order flag_inconsequential_note, after(flag_inconsequential)

// Adjusting country names
replace Country="Czech Rep."  if Country=="Czech Republic"
replace Country="FRG/Germany"  if Country=="Germany"
replace Country="UK"  if Country=="United Kingdom"

// Manual adjustments
replace Month = 4 if Country == "Iceland" & Year == 1991

// Seat shares
forvalues k=1/75 {
	replace Seat_Share_`k' = Seats_`k'/Total_Seats
}

// Reordering variables
order Country Year Month Type_Election Total_Seats
forvalues k = 1/75 {
	order Party_`k' Seats_`k' Seat_Share_`k', last
}
order flag_*, last

// Detecting elections which have the wrong sum of seats (sum of seats /neq total seats) & (sum of seats + appointed + vacant /neq total seats)
egen sum_seats_parties = rowtotal(Seats_*)
egen sum_seats = rowtotal(sum_seats_parties flag_vacant_seats_nb flag_appointed_nb)
replace flag_wrong_sum_seats = 1 if sum_seats_parties!=Total_Seats
replace flag_wrong_sum_seats = . if sum_seats==Total_Seats
drop sum_seats_parties sum_seats

lab var Country  "Country in which election took place"
lab var Year     "Year of the election"
lab var Month    "Month of the election"
lab var Type_Election    "Election type"
lab var flag_wrong_sum_seats		"Sum of elected seats in parliament is inconsistent with the total number of seats"
lab var flag_inconsequential		"Flags cancelled or inconsequential elections"
lab var flag_inconsequential_note		"Why flag_inconsequential is equal to 1"
lab var flag_coup		"Flags elections that were shortly followed by a coup or revolution"
lab var flag_vacant_seats		"Flags elections which left vacant seats in parliament"
lab var flag_vacant_seats_nb		"Number of vacant seats in parliament"
lab var flag_appointed		"Flags elections in which some seats were not elected directly"
lab var flag_appointed_nb		"Number of seats for which MPs were not elected directly"
lab var flag_non_partisan		"Flags elections with no parties"
lab var Total_Seats			"Total number of seats"
forvalues i=1/75 {
	lab var Party_`i'        "Party or coalition n°`i'"
	lab var Seats_`i'  "Seats won by party n°`i'"
	lab var Seat_Share_`i'  "Seat share of party n°`i'"
	replace Seat_Share_`i' = 100*Seat_Share_`i'
}

compress
format Country %30s
format Type_Election %15s
format Party_* %25s
recast double Seat_Share_*
notes drop _dta

save "$project_path/data/2_intermediary/elections/Parliamentary elections/parl_elec_parlgov", replace
